package com.example.chessbattleplatform.Interceptor;

import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component

@Slf4j
@RequiredArgsConstructor
public class WebTokenInterceptor implements HandlerInterceptor {
    private final GetUserTokenInterceptorInformation getUserTokenInterceptorInformation;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 从请求头中获取token (Authorization)
        // 从请求头中获取token (Authorization)
        String token = request.getHeader("Authorization");
        if (token == null) {
            throw new BusinessException(StatusCode.UNAUTHORIZED, "缺少 token");
        }

        return getUserTokenInterceptorInformation.userToken(token);
    }

}
